package com.yanfan.attach.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yanfan.attach.domain.DeviceImgs;
import com.yanfan.attach.service.IDeviceImgsService;
import com.yanfan.common.annotation.Log;
import com.yanfan.common.core.controller.BaseController;
import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.core.page.TableDataInfo;
import com.yanfan.common.enums.BusinessType;
import com.yanfan.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 设备图片Controller
 *
 * @author kerwincui
 * @date 2024-05-06
 */
@RestController
@RequestMapping("/device-imgs")
public class DeviceImgsController extends BaseController {

    @Autowired
    private IDeviceImgsService deviceImgsService;

    /**
     * 查询设备图片列表
     */
    @PreAuthorize("@ss.hasPermi('iot:imgs:list')")
    @GetMapping("/list")
    public TableDataInfo list(DeviceImgs deviceImgs) {
        List<DeviceImgs> list = deviceImgsService
                .lambdaQuery()
                .eq(Objects.nonNull(deviceImgs.getDeviceId()), DeviceImgs::getDeviceId, deviceImgs.getDeviceId())
                .like(StringUtils.isNotEmpty(deviceImgs.getImgName()), DeviceImgs::getImgName, deviceImgs.getImgName())
                .list();
        return getDataTable(list);
    }


    /**
     * 获取设备图片详细信息
     */
    @PreAuthorize("@ss.hasPermi('iot:imgs:query')")
    @GetMapping(value = "/{imgId}")
    public AjaxResult getInfo(@PathVariable("imgId") Long imgId) {
        return success(deviceImgsService.getById(imgId));
    }

    /**
     * 新增设备图片
     */
    @PreAuthorize("@ss.hasPermi('iot:imgs:add')")
    @Log(title = "设备图片", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeviceImgs deviceImgs) {
        return toAjax(deviceImgsService.save(deviceImgs));
    }

    /**
     * 新增设备图片
     */
    @PreAuthorize("@ss.hasPermi('iot:imgs:add')")
    @Log(title = "设备图片", businessType = BusinessType.INSERT)
    @PostMapping("/batch-add")
    public AjaxResult batchAdd(@RequestBody List<DeviceImgs> deviceImgs) {
        if (CollUtil.isNotEmpty(deviceImgs)) {
            deviceImgsService.remove(new LambdaQueryWrapper<DeviceImgs>().eq(DeviceImgs::getDeviceId, deviceImgs
                    .get(0)
                    .getDeviceId()));
        }
        return toAjax(deviceImgsService.saveBatch(deviceImgs));
    }

    /**
     * 修改设备图片
     */
    @PreAuthorize("@ss.hasPermi('iot:imgs:edit')")
    @Log(title = "设备图片", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DeviceImgs deviceImgs) {
        return toAjax(deviceImgsService.update(deviceImgs, new LambdaQueryWrapper<DeviceImgs>()
                .eq(Objects.nonNull(deviceImgs.getDeviceId()), DeviceImgs::getDeviceId, deviceImgs.getDeviceId())
                .eq(Objects.nonNull(deviceImgs.getImgId()), DeviceImgs::getImgId, deviceImgs.getImgId()))

        );
    }

    /**
     * 删除设备图片
     */
    @PreAuthorize("@ss.hasPermi('iot:imgs:remove')")
    @Log(title = "设备图片", businessType = BusinessType.DELETE)
    @DeleteMapping("/{imgIds}")
    public AjaxResult remove(@PathVariable Long[] imgIds) {
        return toAjax(deviceImgsService.removeByIds(Arrays.asList(imgIds)));
    }

}
